﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Threading;
using System.Globalization;
using BIZ;
using DAL;
namespace SinhNhat
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            TextVirtualPathProvider.AppInitialize();
            BIZ.UrlBIZ.GetAllUrl();

            base.Application["count"] = WebUtils.db.COUNTs.FirstOrDefault<COUNT>().HitCounter;
            base.Application["today"] = WebUtils.db.COUNTs.FirstOrDefault<COUNT>().Today;
            WebUtils.db.COUNTs.FirstOrDefault<COUNT>().Date = new DateTime?(DateTime.Today);
            WebUtils.db.SubmitChanges();

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            if (base.Application["online"] != null)
            {
                base.Application["online"] = ((int)base.Application["online"]) + 1;
            }
            else
            {
                base.Application["online"] = 1;
            }
            if (base.Request.Cookies["visited"] == null)
            {
                HttpCookie cookie = new HttpCookie("visited")
                {
                    Expires = DateTime.Now.AddHours(24.0)
                };
                base.Response.Cookies.Add(cookie);
                base.Application.Lock();
                base.Application["count"] = ((int)base.Application["count"]) + 1;
                base.Application.UnLock();
                COUNT count = WebUtils.db.COUNTs.FirstOrDefault<COUNT>();
                count.HitCounter = new int?((int)base.Application["count"]);
                DateTime today = DateTime.Today;
                if (today.Equals(count.Date))
                {
                    base.Application["today"] = ((int)base.Application["today"]) + 1;
                    count.Today = new int?((int)base.Application["today"]);
                }
                else
                {
                    base.Application["today"] = 1;
                    count.Today = 1;
                    count.Date = new DateTime?(today);
                }
                WebUtils.db.SubmitChanges();
            }

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            base.Application.Lock();
            base.Application["online"] = ((int)base.Application["online"]) - 1;
            base.Application.UnLock();

        }

        protected void Application_End(object sender, EventArgs e)
        {
            COUNT count = WebUtils.db.COUNTs.FirstOrDefault<COUNT>();
            count.HitCounter = new int?((int)base.Application["count"]);
            count.Today = new int?((int)base.Application["today"]);
            count.Date = new DateTime?(DateTime.Today);
            WebUtils.db.SubmitChanges();

        }
    }
}